﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebShop.Models;
using System.Web.Security;
using WebShop.DataService;
using WebShop.Classes.Auth;
using WebShop.Classes.DataSource;
using WebShop.Classes;
using WebShop.Classes.Messaging;

namespace WebShop.Controllers
{
    public class LoginController : BaseController
    {
        public IFormsAuthentication FormsAuth { get; private set;}

        public LoginController()
        {
            FormsAuth = new FormsAuthenticationService();
        }

        public ActionResult Index()
        {
            LoginModel model = new LoginModel();
            model.Title = Translater.Translate("title_auth");
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LogOn(string auth_login = "", string auth_password = "")
        {
            if (!String.IsNullOrWhiteSpace(auth_login) && !String.IsNullOrWhiteSpace(auth_password))
            {
                IDataSource dataSource = DataSourceFactory.Contruct();
                User user = dataSource.GetUser(auth_login, auth_password);
                if (user != null)
                {
                    FormsAuth.SignIn(user.Id.ToString(), false);
                    ConfigStorage.User = user;
                    
                    ConfigStorage.Messages.Add(new Message()
                    {
                        Text = String.Format(Translater.Translate("login_auth_success"), user.FIO),
                        Type = MessageType.Info
                    });

                    return RedirectToAction("Index", "Index");
                }
                else
                {
                    ConfigStorage.Messages.Add(new Message()
                    {
                        Text = Translater.Translate("login_auth_error"),
                        Type = MessageType.Error
                    });
                }
            }
            else
            {
                ConfigStorage.Messages.Add(new Message()
                {
                    Text = Translater.Translate("login_incorrect_data"),
                    Type = MessageType.Error
                });
            }
            return RedirectToAction("Index", "Login");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Registration(string login = "", string password = "", string phone = "", string email = "", string fio = "")
        {
            if (!String.IsNullOrWhiteSpace(login) && !String.IsNullOrWhiteSpace(password) && !String.IsNullOrWhiteSpace(phone))
            {

                if (!String.IsNullOrWhiteSpace(email) && !Validate.isEmail(email))
                { 

                    ConfigStorage.Messages.Add(new Message()
                    {
                        Text = Translater.Translate("email_no_valid_fields"),
                        Type = MessageType.Error
                    });

                } else {
                
                    User user = new User()
                    {
                        Id = 0,
                        Email = email,
                        FIO = fio,
                        Login = login,
                        Password = password,
                        Phone = phone,
                        PlaceId = null,
                        Role = UserRole.Guest
                    };

                    user = FormsAuth.SetUser(user);

                    if (user != null)
                    {
                        FormsAuth.SignIn(user.Id.ToString(), false);
                        ConfigStorage.User = user;

                        if (Request.UrlReferrer.ToString() != Request.Url.ToString())
                        {
                            ConfigStorage.Messages.Add(new Message()
                            {
                                Text = Translater.Translate("registration_success"),
                                Type = MessageType.Error
                            });
                            return Redirect(Request.UrlReferrer.ToString());
                        }
                        else
                        {
                            ConfigStorage.Messages.Add(new Message()
                            {
                                Text = Translater.Translate("registration_error"),
                                Type = MessageType.Error
                            });
                            return RedirectToAction("Index", "Index");
                        }
                    }

                }
            }
            else
            {
                ConfigStorage.Messages.Add(new Message()
                {
                    Text = Translater.Translate("not_all_req_fields"),
                    Type = MessageType.Error
                });
            }
            return RedirectToAction("Index", "Login");
        }

        [Authorize]
        public ActionResult Profile()
        {
            ProfileModel model = new ProfileModel();
            model.User = FormsAuth.GetInfo();
            model.Title = Translater.Translate("title_profile");
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Profile(string login = "", string password = "", string phone = "", string email = "", string fio = "")
        {
            if (!String.IsNullOrWhiteSpace(login) && !String.IsNullOrWhiteSpace(phone))
            {
                User user = new User()
                {
                    Id = ConfigStorage.User.Id,
                    Email = email,
                    FIO = fio,
                    Login = login,
                    Phone = phone,
                };

                if (!String.IsNullOrWhiteSpace(password))
                {
                    user.Password = password;
                }

                user = FormsAuth.SetUser(user);

                if (user != null)
                {
                    ConfigStorage.User = user;
                    ConfigStorage.Messages.Add(new Message()
                    {
                        Text = Translater.Translate("profile_edit_success"),
                        Type = MessageType.Error
                    });
                }
                else
                {
                    ConfigStorage.Messages.Add(new Message()
                    {
                        Text = Translater.Translate("profile_edit_error"),
                        Type = MessageType.Error
                    });
                }
            }

            return RedirectToAction("profile", "login");
        }

        public ActionResult Logout()
        {
            FormsAuth.SignOut();
            Session.Remove("user");
            return RedirectToAction("Index", "Index");
        }
    }
}
